<template>
  <!-- 放置弹层组件 -->
  <el-dialog
    :title="form.id ? '编辑部门' : '新增部门'"
    :visible="showDialog"
    width="45%"
    @close="handleCloseDialog"
  >
    <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in peoples"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" size="small" @click="addDept">
        {{ form.id ? "编辑" : "新增" }}
      </el-button>
      <el-button size="small" @click="handleCloseDialog">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departwents'
export default {

  // 需要传入一个props变量来控制 显示或者隐藏
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    curNode: {
      type: Object,
      default: () => ({})
    },
    allTreeData: {
      type: Array,
      default: () => []
    }
  },
  data () {
    const validateCode = (rule, value, callback) => {
      let flag
      if (this.form.id) {
        // 编辑
        flag = this.allTreeData.some(item => {
          // value !== this.curNode.code 编辑时排除自身
          return item.code === value && value !== this.curNode.code
        })
      } else {
        // 新增
        flag = this.allTreeData.some(item => {
          return item.code === value
        })
      }
      // 遍历所有部门数据匹配
      if (flag) {
        callback(new Error('当前code码重复'))
      } else {
        callback()
      }
    }
    return {
      // 部门负责人选择列表数据
      peoples: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 2, max: 50, message: '部门名称要求2-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 2, max: 50, message: '部门编码要求2-50个字符', trigger: ['blur', 'change'] },
          { validator: validateCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 2, max: 300, message: '部门介绍要求2-300个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.hGetEmployeeSimple()
  },
  methods: {
    handleCloseDialog () {
      this.$emit('close-dialog')
    },

    // 获取员工简单列表数据
    async hGetEmployeeSimple () {
      const data = await getEmployeeSimple()
      console.log(data)
      this.peoples = data
    },
    // 数据回填
    async hGetDepartDetail (id) {
      const data = await getDepartDetail(id)
      this.form = data
    },
    // 提交
    addDept () {
      // 先进行兜底校验
      this.$refs.deptForm.validate(async (valid) => {
        // valid：全部通过为true  否则为false  自动把不满足规则的表单都标红
        if (valid) {
          // 发起接口调用
          // 准备参数
          // form和传下来的id汇总成一个完整的参数对象
          if (this.form.id) {
            // 调用更新接口
            await updateDepartments(this.form)
            this.$message.success('更新成功！')
          } else {
            // 调用新增接口
            await addDepartments({ ...this.form, pid: this.curNode.id || '' })
            this.$message.success('添加成功！')
          }
          // 1. 关闭弹框
          this.$emit('close-dialog')
          // 2. 通知父组件去重新拉取最新的数据
          this.$emit('update-dept')
        } else {
          return false
        }
      })
    },
    closeDialog () {
      // 通知父组件把控制弹框显示的数据设置为false
      this.$emit('close-dialog')
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      // 清除校验残留
      this.$refs.deptForm.resetFields()
    }

  }

}
</script>

<style lang='scss' scoped>
</style>
